package com.lhj.xxsc.order.listener;

import com.lhj.xxsc.order.entity.OrderEntity;
import com.lhj.xxsc.order.service.OrderService;
import com.rabbitmq.client.Channel;
import com.xxsc.common.to.mq.SeckillOrderTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author 罗惠江
 * @version 1.0
 * @email 1511619294@qq.com
 * @date 2021/1/14 13:14
 */
//@RestController
@Slf4j
@Component
@RabbitListener(queues = "order.seckill.order.queue")
public class OrderSeckillListener {
	@Autowired
	OrderService orderService;

	@RabbitHandler
	public void handleStockLockedRelease(SeckillOrderTo entity, Message message, Channel channel) throws IOException {
		System.out.println("收到秒杀订单" + entity.getOrderSn());
		try {
			orderService.createSeckillOrder(entity);
			// 手动调用支付宝收单
			// TODO
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
		}

	}
}
